package com.mc.mcgoweb.thread.threaddaemon;

public class DaemonThread {
    /**
     * 守护线程
     * 如果主线程生命周期结束，那么守护线程相应结束，如果非守护线程，那么会一直执行
     * @param args
     */
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            Thread innerThread = new Thread(() -> {
                while (true) {
                    System.out.println("心跳线程正在执行.");
                    try {
                        Thread.sleep(1_000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            innerThread.setDaemon(true);//如果非守护线程，那么jvm不会关闭
            innerThread.start();
        });
        try {
            thread.start();
            Thread.sleep(1_000);
            System.out.println("非守护线程执行完成。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
